package com.my.service.imp;

import com.my.dao.BusinessDao;
import com.my.dao.GoodsDao;
import com.my.dao.OrdersDao;
import com.my.pojo.Address;
import com.my.pojo.Business;
import com.my.pojo.Goods;
import com.my.pojo.Orders;
import com.my.service.IOrdersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;


@Service
public class OrdersServiceImpl implements IOrdersService {
    OrdersDao ordersDao;

    GoodsDao goodsDao;

    @Autowired
    public void setGoodsDao(GoodsDao goodsDao) {
        this.goodsDao = goodsDao;
    }

    @Autowired
    public void setOrdersDao(OrdersDao ordersDao) {
        this.ordersDao = ordersDao;
    }

    @Override
    public Page<Orders> showAllOrders(Pageable pageable, String context, Integer businessId) {
        return ordersDao.findAll(
                (root, cq, cb) -> cb.equal(root.get("business"), businessId)
                , pageable);
    }

    @Override
    public Integer sendOrders(Integer ordersId, Integer businessId) {
        Orders orders = ordersDao.findOne(ordersId);
        if(orders == null || orders.getStatus() == -1) {
            return -1;
        }

        if(orders.getBusiness() == null || orders.getBusiness().getBusinessId().equals(businessId)) {
            return -4;
        }

        if(orders.getType() != 0) {
            return -2;
        }

        orders.setType(1);
        return ordersDao.save(orders) == null ? -3 : 0;
    }

    @Override
    public Integer deleteOrders(Integer ordersId, Integer businessId) {
        Orders orders = ordersDao.findOne(ordersId);
        if(orders == null || orders.getStatus() == -1) {
            return -1;
        }

        if(orders.getBusiness() == null || orders.getBusiness().getBusinessId() != businessId) {
            return -3;
        }

        orders.setStatus(-1);
        return ordersDao.save(orders) == null ? -2 : 0;
    }

    @Override
    public Integer addOrdersLogistics(Integer ordersId, String logistics, Integer businessId) {
        Orders orders = ordersDao.getOne(ordersId);
        if(orders == null ||  orders.getStatus() == -1) {
            return -1;
        }

        if(orders.getType() == 0) {
            return -2;
        }

        if(orders.getBusiness() == null || orders.getBusiness().getBusinessId() != businessId) {
            return -4;
        }

        orders.setLogistics(logistics);
        orders.setType(2);
        return ordersDao.save(orders) == null ? -3 : 0;
    }

    @Override
    public Integer addOrder(Orders orders, Integer goodsId) {
        Goods goods = goodsDao.findOne(goodsId);

        if(goods == null || goods.getStatus() == -1) {
            return -1;
        }

        Business business = new Business();
        business.setBusinessId(goods.getBusiness().getBusinessId());

        orders.setBusiness(business);
        Orders res = ordersDao.save(orders);
        return res == null ? -2 : res.getOrderId();
    }

    @Override
    public Integer addAddress(Integer ordersId, Integer addressId, Integer userId) {
        Orders orders = ordersDao.findOne(ordersId);
        if(orders == null || orders.getStatus() != 0) {
            return -1;
        }

        if(orders.getUser() == null || !orders.getUser().getUserId().equals(userId)) {
            return -2;
        }

        Address address = new Address();
        address.setAddress_id(addressId);

        orders.setAddress(address);
        orders.setType(1);
        return ordersDao.save(orders) == null ? -3 : 1;
    }

    @Override
    public Page<Orders> showAllOrdersForUser(Pageable pageable, String context, Integer userId) {
        return ordersDao.findAll(
                (root, cq, cb) -> cb.equal(root.get("user"), userId)
                , pageable);
    }

    @Override
    public Orders showOneOrder(Integer ordersId, Integer userId) {
        return ordersDao.findOne(
                (root, cq, cb) -> cb.and(
                        cb.equal(root.get("orderId"), ordersId),
                        cb.equal(root.get("user"), userId)
                )
        );
    }

    @Override
    public Integer confirm(Integer ordersId, Integer userId) {
        Orders orders = ordersDao.findOne(
                (root, cq, cb) -> cb.and(
                        cb.equal(root.get("orderId"), ordersId),
                        cb.equal(root.get("user"), userId)
                ));

        if(orders != null && orders.getStatus() == 0) {
            orders.setType(4);
            return ordersDao.save(orders) == null ? -2 : 1;
        } else {
            return -1;
        }
    }


}
